﻿// Copyright (c) 2012, Alexander Endris
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
// 
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#region Usings

using System;
using System.Drawing;
using System.Drawing.Imaging;
using NamelessOne.Imaging;
using Image = NamelessOne.Imaging.Image;
using PixelFormat = NamelessOne.Imaging.PixelFormat;

#endregion

namespace ImagingTest
{
  internal static class Program
  {
    #region Public / Methods

    public static void TestImage()
    {
      DateTime start = DateTime.Now;
      Image img = Image.LoadFromFile(@"E:\important\Desktop\Irelia.jpg");
      DateTime b = DateTime.Now;
      Console.Write("Read: ");
      Console.WriteLine(b - start);
      GetPixel(img);
      DateTime final = DateTime.Now;
      Console.Write("Written (GetPixel): ");
      Console.WriteLine(final - b);
      Console.Write("Total: ");
      Console.WriteLine(final - start);
      ReadScanline(img);
      DateTime final2 = DateTime.Now;
      Console.Write("Written (ReadScanline): ");
      Console.WriteLine(final2 - final);
      Console.Write("Total: ");
      Console.WriteLine(final2 - start);
    }

    #endregion

    #region Private / Methods

    private static void GetPixel(Image img)
    {
      Bitmap bmp = new Bitmap(img.Width, img.Height);

      using (Graphics g = Graphics.FromImage(bmp))
      {
        for (int y = 0; y < img.Height; y++)
        {
          for (int x = 0; x < img.Width; x++)
          {
            Color16 c = img.GetPixel((uint) x, (uint) y);

            bmp.SetPixel(x, y, c);
          }
        }
      }
      bmp.Save(@"E:\Important\Desktop\Test.png", ImageFormat.Png);
    }

    /// <summary>
    ///   The main entry point for the application.
    /// </summary>
    [STAThread]
    private static void Main()
    {
      TestImage();
    }

    private static void ReadScanline(Image img)
    {
      Bitmap bmp = new Bitmap(img.Width, img.Height);

      byte[] scanline;
      using (Graphics g = Graphics.FromImage(bmp))
      {
        for (int y = 0; y < img.Height; y++)
        {
          img.ReadNextScanline(out scanline);

          for (int x = 0; x < img.Width; x++)
          {
            Color16 c;

            switch (img.PixelFormat)
            {
              case PixelFormat.Greyscale8:
                c = new Color16(scanline[x], 1);
                break;
              case PixelFormat.Greyscale16:
                c = new Color16(scanline[x*2] | scanline[x*2 + 1] << 8, 2);
                break;
              case PixelFormat.Rgb16:
                c = Color16.FromRGB565(scanline[x*2], scanline[x*2 + 1]);
                break;
              case PixelFormat.Rgb24:
                c = new Color16(scanline[x*3], scanline[x*3 + 1], scanline[x*3 + 2], (byte) 1);
                break;
              case PixelFormat.Argb32:
                c = new Color16(scanline[x*3], scanline[x*3 + 1], scanline[x*3 + 2], scanline[x*3 + 3], 1);
                break;
              default:
                throw new ArgumentOutOfRangeException();
            }

            bmp.SetPixel(x, y, c);
          }
        }
      }
      bmp.Save(@"E:\Important\Desktop\Test.png", ImageFormat.Png);
    }

    #endregion
  }
}